package com.clippick.ui;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class PieChart extends View {
	
	private static final int WAIT = 0;
	private static final int IS_READY_TO_DRAW = 1;
	private static final int IS_DRAW = 2;
	private static final float START_INC = 30;
	
	private Paint backgroundPaint = new Paint();
	private Paint linePaint = new Paint();
	
	private int mOverlayId;
	private int mWidth;
	private int mHeight;
	private int mGapLeft;
	private int mGapRight;
	private int mGapTop;
	private int mGapBottom;
	private int mBgColor;
	private int mState = WAIT;
	private float mStart;
	private float mSweep;
	private int mMaxConnection;
	
	private List<PieItem> mDataArray;

	public PieChart(Context context) {
		super(context);
	}

	public PieChart(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (mState != IS_READY_TO_DRAW)
			return;

		canvas.drawColor(mBgColor);
		backgroundPaint.setAntiAlias(true);
		backgroundPaint.setStyle(Paint.Style.FILL);
		backgroundPaint.setColor(0x88FF0000);
		backgroundPaint.setStrokeWidth(0.5f);

		linePaint.setAntiAlias(true);
		linePaint.setStyle(Paint.Style.STROKE);
		linePaint.setColor(0xff000000);
		linePaint.setStrokeWidth(0.5f);

		RectF ovals = new RectF(mGapLeft, mGapTop, mWidth - mGapRight, mHeight
				- mGapBottom);
		mStart = START_INC;
		PieItem item;

		for (int i = 0; i < mDataArray.size(); i++) {
			item = (PieItem) mDataArray.get(i);
			backgroundPaint.setColor(item.Color);
			mSweep = (float) 360
					* ((float) item.Count / (float) mMaxConnection);
			canvas.drawArc(ovals, mStart, mSweep, true, backgroundPaint);
			mStart += mSweep;
		}

		Options options = new BitmapFactory.Options();
		options.inScaled = false;
		Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(),
				mOverlayId, options);
		canvas.drawBitmap(overlayBitmap, 0.0f, 0.0f, null);
		mState = IS_DRAW;
	}

	public void setGeometry(int width, int height, int gapLeft, int gapRight,
			int gapTop, int gapBottom, int overlayId) {
		mWidth = width;
		mHeight = height;
		mGapLeft = gapLeft;
		mGapRight = gapRight;
		mGapTop = gapTop;
		mGapBottom = gapBottom;
		mOverlayId = overlayId;
	}

	public void setSkinParams(int backgroundColor) {
		mBgColor = backgroundColor;
	}

	public void setData(List<PieItem> data, int maxConnection) {
		mDataArray = data;
		mMaxConnection = maxConnection;
		mState = IS_READY_TO_DRAW;
	}

	public void setState(int state) {
		mState = state;
	}

	public int getColorValue(int index) {
		if (mDataArray == null)
			return 0;
		
		if (index < 0) {
			return ((PieItem) mDataArray.get(0)).Color;
		} else if (index >= mDataArray.size()) {
			return ((PieItem) mDataArray.get(mDataArray.size() - 1)).Color;
		} else {
			return ((PieItem) mDataArray.get(mDataArray.size() - 1)).Color;
		}
	}
}